package org.superheroes.androidserver.thread;

public abstract class TimerThread implements Runnable
{
	private long lastUpdate;
	private long interval;
	
	private boolean running = false;
	
	public TimerThread( long interval )
	{
		this.interval = interval;
		lastUpdate = getTime();
	}
	
	private long getTime()
	{
		return System.currentTimeMillis();
	}
	
	public void start()
	{
		running = true;
		
		run();
	}
	
	public void stop()
	{
		running = false;
	}
	
	@Override
	public void run()
	{
		while( running )
		{
			updateTicksPerSecond();
		}
	}
	
	private void updateTicksPerSecond()
	{
		if( ( getTime() - lastUpdate ) > interval )
		{
			performAction();
			
			lastUpdate += interval;
		}
	}
	
	public abstract void performAction();
}
